package com.hp.controller;

import com.hp.annotation.Power;
import com.hp.entity.ResourceTypeTreeVo;
import com.hp.entity.Resources;
import com.hp.result.DataGridResultInfo;
import com.hp.result.SubmitResultInfo;
import com.hp.service.ResourcesService;
import com.hp.service.ResourcesTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/resources")
@Power(name = "资源管理",type =0)
public class ResourcesController {

    @Autowired
    private ResourcesService resourcesService;


    @Autowired
    private ResourcesTypeService resourcesTypeService;


    /**
     * 去添加资源页面
     *
     * @return
     * @throws Exception
     */
    @Power(name = "去添加资源页面",type =1,url = "/resources/toaddresource")
    @RequestMapping("toaddresource")
    public String toaddresource(Map<String, Object> map) throws Exception {
        List<Resources> list = resourcesTypeService.pageresourcetype(1, Integer.MAX_VALUE, null).getRows();
        map.put("typelist", list);
        return "resources/addresource";
    }

    /**
     * 添加资源
     *
     * @param myfile
     * @param resources
     * @param request
     * @return
     * @throws Exception
     */
    @Power(name = "添加资源",type =1,url = "/resources/addresourcesubmit")
    @ResponseBody
    @RequestMapping(value = "addresourcesubmit", method = RequestMethod.POST)
    public SubmitResultInfo addresourcesubmit(
            @RequestParam(value = "myfile", required = false) CommonsMultipartFile myfile,
            Resources resources,
            HttpServletRequest request) throws Exception {

        return resourcesService.saveresource(myfile,
                resources, request);
    }


    /**
     * 去资源审核页面
     *
     * @return
     */
    @Power(name = "去资源审核页面",type =1,url = "/resources/queryresoures")
    @GetMapping(value = "queryresoures")
    public String queryresoures() throws Exception {

        return "resources/examine/queryresoures";
    }


    /**
     * 查看所有审核的资源
     *
     * @param page
     * @param rows
     * @param resources
     * @return
     * @throws Exception
     */
    @Power(name = "查看所有审核的资源",type =1,url = "/resources/getAllAndIsN")
    @ResponseBody
    @RequestMapping("/getAllAndIsN")
    public DataGridResultInfo getAllAndIsN(
            @RequestParam(name = "page", defaultValue = "1") int page,
            @RequestParam(name = "rows", defaultValue = "10") int rows,
            Resources resources) throws Exception {

        return resourcesService.getAllAndIsN(page, rows,
                resources);
    }


    /**
     * 去修改界面
     *
     * @param id
     * @param model
     * @return
     * @throws Exception
     */
    @Power(name = "去修改界面",type =1,url = "/resources/toupdate")
    @GetMapping("/toupdate")
    public String toupdate(int id, Model model) throws Exception {
        Resources resources = resourcesService.findByid(id);
        model.addAttribute("typelist", resourcesTypeService.pageresourcetype(1, Integer.MAX_VALUE, null).getRows());
        model.addAttribute("psr", resources);
        return "resources/updateresource";
    }

    /**
     * 修改
     *
     * @param myfile
     * @param resources
     * @param request
     * @return
     * @throws Exception
     */
    @Power(name = "修改资源",type =1,url = "/resources/updateresourcesubmit")
    @ResponseBody
    @PostMapping(value = "updateresourcesubmit")
    public SubmitResultInfo updateresourcesubmit(@RequestParam(value = "myfile", required = false) CommonsMultipartFile myfile,
                                                 Resources resources,
                                                 HttpServletRequest request) throws Exception {
        return resourcesService.update(myfile,
                resources, request);
    }

    @Power(name = "修改资源",type =1,url = "/resources/adoptpsr")
    @ResponseBody
    @PostMapping(value = "adoptpsr")
    public SubmitResultInfo adoptpsr(String id) throws Exception {
        String[] str = id.split(",");
        Resources resources = new Resources();
        resources.setId(Integer.parseInt(str[0]));
        resources.setStatus(Integer.parseInt(str[1]));
        return resourcesService.adoptpsr(resources);
    }


    /**
     * 审核列表删除
     *
     * @param id
     * @return
     */
    @Power(name = "审核列表删除",type =1,url = "/resources/deleteIsNpsr")
    @ResponseBody
    @PostMapping(value = "deleteIsNpsr")
    public SubmitResultInfo deleteIsNpsr(int id) throws Exception {
        return resourcesService.deleteIsNpsr(id);
    }

    /**
     * 去科普资源页面
     * @return
     */
    @Power(name = "去科普资源页面",type =1,url = "/resources/resourcesTree")
    @RequestMapping("/resourcesTree")
    public String resourcesTree() {
        return "resources/resourcesTree";
    }

    /*
     * 生成类型的树
     */
    @Power(name = "生成类型的树",type =1,url = "/resources/getResourceTypeTreeVo")
    @RequestMapping("/getResourceTypeTreeVo")
    @ResponseBody
    public List<ResourceTypeTreeVo> getResourceTreeVo() throws Exception {
        List<ResourceTypeTreeVo> treeVos = new ArrayList<ResourceTypeTreeVo>();
        treeVos.add(resourcesTypeService.getResourceTypeTreeVo());
        return treeVos;
    }

    /**
     * 资源树点击右边列表
     * @param typeid
     * @param map
     * @return
     * @throws Exception
     */
    @Power(name = "资源树点击右边页面表",type =1,url = "/resources/queryPSRById")
    @GetMapping("queryPSRById")
    public String queryPSRById(String typeid, Map<String, Object> map)
            throws Exception {
        map.put("typeid", typeid);
        return "resources/queryresourcebyid";
    }

    @Power(name = "资源树点击右边列表",type =1,url = "/resources/getAllById")
    @ResponseBody
    @RequestMapping("getAllById")
    public DataGridResultInfo getAllById(String typeid,
                                         @RequestParam(name = "page", defaultValue = "1") int page,
                                         @RequestParam(name = "rows", defaultValue = "10") int rows,
                                         Resources resources) throws Exception {
        return resourcesService.getAllById(typeid, page, rows,
                resources);
    }
}
